home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 April / macformat-049.iso / mac / Shareware Plus / Developers / dropg++ / usr / src / loader / standload.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-15  |  69.9 KB  |  2,258 lines  |  [TEXT/KAHL]

  1. #pragma options (!check_ptrs)
  2. #include <string.h>
  3. #include <Resources.h>
  4.  
  5. /* Linker `ld' for GNU
  6.    Copyright (C) 1988 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 1, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* Written by Richard Stallman with some help from Eric Albert.
  23.    Set, indirect, and warning ld_symbol features added by Randy Smith.  */
  24.    
  25. /* Define how to initialize system-dependent header fields.  */
  26. #ifdef sun
  27. /* Use Sun's TARGET convention.  */
  28. #ifndef TARGET
  29. #define SUN2 2
  30. #define SUN3 3
  31. #define SUN4 4
  32. #if defined(sparc)
  33. #define TARGET SUN4
  34. #else
  35. #if defined(mc68020) || defined(m68020)
  36. #define TARGET SUN3
  37. #else
  38. #define TARGET SUN2
  39. #endif
  40. #endif
  41. #else
  42. #define _CROSS_TARGET_ARCH TARGET  /* locate the correct a.out.h file */
  43. #endif
  44. #endif
  45.  
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #include <sys/file.h>
  49. #ifndef sony_news
  50. #include <sys/fcntl.h>
  51. #endif
  52.  
  53. #ifdef COFF_ENCAPSULATE
  54. #include "a.out.encap.h"
  55. #else
  56. #include <a.out.h>
  57. #endif
  58.  
  59. #ifndef N_SET_MAGIC
  60. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  61. #endif
  62.  
  63. /* Always use the GNU version of debugging ld_symbol type codes, if possible.  */
  64.  
  65. #include "stab.h"
  66. #define CORE_ADDR unsigned long    /* For symseg.h */
  67. /* GDB symbol table format definitions.
  68.    Copyright (C) 1986, 1989 Free Software Foundation, Inc.
  69.    Hacked by Michael Tiemann (tiemann@mcc.com)
  70.  
  71. This file is part of GDB.
  72.  
  73. GDB is free software; you can redistribute it and/or modify
  74. it under the terms of the GNU General Public License as published by
  75. the Free Software Foundation; either version 1, or (at your option)
  76. any later version.
  77.  
  78. GDB is distributed in the hope that it will be useful,
  79. but WITHOUT ANY WARRANTY; without even the implied warranty of
  80. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  81. GNU General Public License for more details.
  82.  
  83. You should have received a copy of the GNU General Public License
  84. along with GDB; see the file COPYING.  If not, write to
  85. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  86.  
  87. /* Format of GDB symbol table data.
  88.    There is one symbol segment for each source file or
  89.    independant compilation.  These segments are simply concatenated
  90.    to form the GDB symbol table.  A zero word where the beginning
  91.    of a segment is expected indicates there are no more segments.
  92.  
  93. Format of a symbol segment:
  94.  
  95.    The symbol segment begins with a word containing 1
  96.    if it is in the format described here.  Other formats may
  97.    be designed, with other code numbers.
  98.  
  99.    The segment contains many objects which point at each other.
  100.    The pointers are offsets in bytes from the beginning of the segment.
  101.    Thus, each segment can be loaded into core and its pointers relocated
  102.    to make valid in-core pointers.
  103.  
  104.    All the data objects in the segment can be found indirectly from
  105.    one of them, the root object, of type `struct symbol_root'.
  106.    It appears at the beginning of the segment.
  107.  
  108.    The total size of the segment, in bytes, appears as the `length'
  109.    field of this object.  This size includes the size of the
  110.    root object.
  111.  
  112.    All the object data types are defined here to contain pointer types
  113.    appropriate for in-core use on a relocated symbol segment.
  114.    Casts to and from type int are required for working with
  115.    unrelocated symbol segments such as are found in the file.
  116.  
  117.    The ldsymaddr word is filled in by the loader to contain
  118.    the offset (in bytes) within the ld symbol table
  119.    of the first nonglobal symbol from this compilation.
  120.    This makes it possible to match those symbols
  121.    (which contain line number information) reliably with
  122.    the segment they go with.
  123.  
  124.    Core addresses within the program that appear in the symbol segment
  125.    are not relocated by the loader.  They are inserted by the assembler
  126.    and apply to addresses as output by the assembler, so GDB must
  127.    relocate them when it loads the symbol segment.  It gets the information
  128.    on how to relocate from the textrel, datarel, bssrel, databeg and bssbeg
  129.    words of the root object.
  130.  
  131.    The words textrel, datarel and bssrel
  132.    are filled in by ld with the amounts to relocate within-the-file
  133.    text, data and bss addresses by; databeg and bssbeg can be
  134.    used to tell which kind of relocation an address needs.  */
  135.  
  136. enum language {language_c};
  137.  
  138. struct symbol_root
  139. {
  140.   int format;            /* Data format version */
  141.   int length;            /* # bytes in this symbol segment */
  142.   int ldsymoff;            /* Offset in ld symtab of this file's syms */
  143.   int textrel;            /* Relocation for text addresses */
  144.   int datarel;            /* Relocation for data addresses */
  145.   int bssrel;            /* Relocation for bss addresses */
  146.   char *filename;        /* Name of main source file compiled */
  147.   char *filedir;        /* Name of directory it was reached from */
  148.   struct blockvector *blockvector; /* Vector of all symbol-naming blocks */
  149.   struct typevector *typevector; /* Vector of all data types */
  150.   enum language language;    /* Code identifying the language used */
  151.   char *version;        /* Version info.  Not fully specified */
  152.   char *compilation;        /* Compilation info.  Not fully specified */
  153.   int databeg;            /* Address within the file of data start */
  154.   int bssbeg;            /* Address within the file of bss start */
  155.   struct sourcevector *sourcevector; /* Vector of line-number info */
  156. };
  157.  
  158. /* All data types of symbols in the compiled program
  159.    are represented by `struct type' objects.
  160.    All of these objects are pointed to by the typevector.
  161.    The type vector may have empty slots that contain zero.  */
  162.  
  163. struct typevector
  164. {
  165.   int length;            /* Number of types described */
  166.   struct type *type[1];
  167. };
  168.  
  169. /* Different kinds of data types are distinguished by the `code' field.  */
  170.  
  171. enum type_code
  172. {
  173.   TYPE_CODE_UNDEF,        /* Not used; catches errors */
  174.   TYPE_CODE_PTR,        /* Pointer type */
  175.   TYPE_CODE_ARRAY,        /* Array type, lower bound zero */
  176.   TYPE_CODE_STRUCT,        /* C struct or Pascal record */
  177.   TYPE_CODE_UNION,        /* C union or Pascal variant part */
  178.   TYPE_CODE_ENUM,        /* Enumeration type */
  179.   TYPE_CODE_FUNC,        /* Function type */
  180.   TYPE_CODE_INT,        /* Integer type */
  181.   TYPE_CODE_FLT,        /* Floating type */
  182.   TYPE_CODE_VOID,        /* Void type (values zero length) */
  183.   TYPE_CODE_SET,        /* Pascal sets */
  184.   TYPE_CODE_RANGE,        /* Range (integers within spec'd bounds) */
  185.   TYPE_CODE_PASCAL_ARRAY,    /* Array with explicit type of index */
  186.  
  187.   /* C++ */
  188.   TYPE_CODE_MEMBER,        /* Member type */
  189.   TYPE_CODE_METHOD,        /* Method type */
  190.   TYPE_CODE_REF        /* C++ Reference types */
  191. };
  192.  
  193. /* This appears in a type's flags word for an unsigned integer type.  */
  194. #define TYPE_FLAG_UNSIGNED 1
  195. /* This appears in a type's flags word
  196.    if it is a (pointer to a|function returning a)* built in scalar type.
  197.    These types are never freed.  */
  198. #define TYPE_FLAG_PERM 4
  199. /* This appears in a type's flags word if it is a stub type (eg. if
  200.    someone referenced a type that wasn't definined in a source file
  201.    via (struct sir_not_appearing_in_this_film *)).  */
  202. #define TYPE_FLAG_STUB 8
  203. /* Set when a classic has a constructor defined */
  204. #define    TYPE_FLAG_HAS_CONSTRUCTOR    256
  205. /* Set when a classic has a destructor defined */
  206. #define    TYPE_FLAG_HAS_DESTRUCTOR    512
  207. /* Indicates that this type is a public baseclass of another classic,
  208.    i.e. that all its public methods are available in the derived
  209.    classic. */
  210. #define    TYPE_FLAG_VIA_PUBLIC        1024
  211. /* Indicates that this type is a virtual baseclass of another classic,
  212.    i.e. that if this classic is inherited more than once by another
  213.    classic, only one set of member variables will be included. */ 
  214. #define    TYPE_FLAG_VIA_VIRTUAL        2048
  215.  
  216. struct type
  217. {
  218.   /* Code for kind of type */
  219.   enum type_code code;
  220.   /* Name of this type, or zero if none.
  221.      This is used for printing only.
  222.      Type names specified as input are defined by symbols.  */
  223.   char *name;
  224.   /* Length in bytes of storage for a value of this type */
  225.   int length;
  226.   /* For a pointer type, describes the type of object pointed to.
  227.      For an array type, describes the type of the elements.
  228.      For a function or method type, describes the type of the value.
  229.      For a range type, describes the type of the full range.
  230.      Unused otherwise.  */
  231.   struct type *target_type;
  232.   /* Type that is a pointer to this type.
  233.      Zero if no such pointer-to type is known yet.
  234.      The debugger may add the address of such a type
  235.      if it has to construct one later.  */ 
  236.   struct type *pointer_type;
  237.   /* C++: also need a reference type.  */
  238.   struct type *reference_type;
  239.   struct type **arg_types;
  240.   
  241.   /* Type that is a function returning this type.
  242.      Zero if no such function type is known here.
  243.      The debugger may add the address of such a type
  244.      if it has to construct one later.  */
  245.   struct type *function_type;
  246.  
  247. /* Handling of pointers to members:
  248.    TYPE_MAIN_VARIANT is used for pointer and pointer
  249.    to member types.  Normally it the value of the address of its
  250.    containing type.  However, for pointers to members, we must be
  251.    able to allocate pointer to member types and look them up
  252.    from some place of reference.
  253.    NEXT_VARIANT is the next element in the chain. */
  254.   struct type *main_variant, *next_variant;
  255.  
  256.   /* Flags about this type.  */
  257.   short flags;
  258.   /* Number of fields described for this type */
  259.   short nfields;
  260.   /* For structure and union types, a description of each field.
  261.      For set and pascal array types, there is one "field",
  262.      whose type is the domain type of the set or array.
  263.      For range types, there are two "fields",
  264.      the minimum and maximum values (both inclusive).
  265.      For enum types, each possible value is described by one "field".
  266.  
  267.      Using a pointer to a separate array of fields
  268.      allows all types to have the same size, which is useful
  269.      because we can allocate the space for a type before
  270.      we know what to put in it.  */
  271.   struct field
  272.     {
  273.       /* Position of this field, counting in bits from start of
  274.      containing structure.  For a function type, this is the
  275.      position in the argument list of this argument.
  276.      For a range bound or enum value, this is the value itself.  */
  277.       int bitpos;
  278.       /* Size of this field, in bits, or zero if not packed.
  279.      For an unpacked field, the field's type's length
  280.      says how many bytes the field occupies.  */
  281.       int bitsize;
  282.       /* In a struct or enum type, type of this field.
  283.      In a function type, type of this argument.
  284.      In an array type, the domain-type of the array.  */
  285.       struct type *type;
  286.       /* Name of field, value or argument.
  287.      Zero for range bounds and array domains.  */
  288.       char *name;
  289.     } *fields;
  290.  
  291.   /* C++ */
  292.   int *private_field_bits;
  293.   int *protected_field_bits;
  294.  
  295.   /* Number of methods described for this type */
  296.   short nfn_fields;
  297.   /* Number of base classes this type derives from. */
  298.   short n_baseclasses;
  299.  
  300.   /* Number of methods described for this type plus all the
  301.      methods that it derives from.  */
  302.   int nfn_fields_total;
  303.  
  304.   /* For classes, structures, and unions, a description of each field,
  305.      which consists of an overloaded name, followed by the types of
  306.      arguments that the method expects, and then the name after it
  307.      has been renamed to make it distinct.  */
  308.   struct fn_fieldlist
  309.     {
  310.       /* The overloaded name.  */
  311.       char *name;
  312.       /* The number of methods with this name.  */
  313.       int length;
  314.       /* The list of methods.  */
  315.       struct fn_field
  316.     {
  317. #if 0
  318.       /* The overloaded name */
  319.       char *name;
  320. #endif
  321.       /* The return value of the method */
  322.       struct type *type;
  323.       /* The argument list */
  324.       struct type **args;
  325.       /* The name after it has been processed */
  326.       char *physname;
  327.       /* If this is a virtual function, the offset into the vtbl-1,
  328.          else 0.  */
  329.       int voffset;
  330.     } *fn_fields;
  331.  
  332.       int *private_fn_field_bits;
  333.       int *protected_fn_field_bits;
  334.  
  335.     } *fn_fieldlists;
  336.  
  337.   unsigned char via_protected;
  338.   unsigned char via_public;
  339.  
  340.   /* For types with virtual functions, VPTR_BASETYPE is the base classic which
  341.      defined the virtual function table pointer.  VPTR_FIELDNO is
  342.      the field number of that pointer in the structure.
  343.  
  344.      For types that are pointer to member types, VPTR_BASETYPE
  345.      ifs the type that this pointer is a member of.
  346.  
  347.      Unused otherwise.  */
  348.   struct type *vptr_basetype;
  349.  
  350.   int vptr_fieldno;
  351.  
  352.   /* If this type has a base classic, put it here.
  353.      If this type is a pointer type, the chain of member pointer
  354.      types goes here.
  355.      Unused otherwise.
  356.      
  357.      Contrary to all maxims of C style and common sense, the baseclasses
  358.      are indexed from 1 to N_BASECLASSES rather than 0 to N_BASECLASSES-1
  359.      (i.e. BASECLASSES points to one *before* the first element of
  360.      the array).  */
  361.   struct type **baseclasses;
  362. };
  363.  
  364. /* All of the name-scope contours of the program
  365.    are represented by `struct block' objects.
  366.    All of these objects are pointed to by the blockvector.
  367.  
  368.    Each block represents one name scope.
  369.    Each lexical context has its own block.
  370.  
  371.    The first two blocks in the blockvector are special.
  372.    The first one contains all the symbols defined in this compilation
  373.    whose scope is the entire program linked together.
  374.    The second one contains all the symbols whose scope is the
  375.    entire compilation excluding other separate compilations.
  376.    In C, these correspond to global symbols and static symbols.
  377.  
  378.    Each block records a range of core addresses for the code that
  379.    is in the scope of the block.  The first two special blocks
  380.    give, for the range of code, the entire range of code produced
  381.    by the compilation that the symbol segment belongs to.
  382.  
  383.    The blocks appear in the blockvector
  384.    in order of increasing starting-address,
  385.    and, within that, in order of decreasing ending-address.
  386.  
  387.    This implies that within the body of one function
  388.    the blocks appear in the order of a depth-first tree walk.  */
  389.  
  390. struct blockvector
  391. {
  392.   /* Number of blocks in the list.  */
  393.   int nblocks;
  394.   /* The blocks themselves.  */
  395.   struct block *block[1];
  396. };
  397.  
  398. struct block
  399. {
  400.   /* Addresses in the executable code that are in this block.
  401.      Note: in an unrelocated symbol segment in a file,
  402.      these are always zero.  They can be filled in from the
  403.      N_LBRAC and N_RBRAC symbols in the loader symbol table.  */
  404.   int startaddr, endaddr;
  405.   /* The symbol that names this block,
  406.      if the block is the body of a function;
  407.      otherwise, zero.
  408.      Note: In an unrelocated symbol segment in an object file,
  409.      this field may be zero even when the block has a name.
  410.      That is because the block is output before the name
  411.      (since the name resides in a higher block).
  412.      Since the symbol does point to the block (as its value),
  413.      it is possible to find the block and set its name properly.  */
  414.   struct symbol *function;
  415.   /* The `struct block' for the containing block, or 0 if none.  */
  416.   /* Note that in an unrelocated symbol segment in an object file
  417.      this pointer may be zero when the correct value should be
  418.      the second special block (for symbols whose scope is one compilation).
  419.      This is because the compiler ouptuts the special blocks at the
  420.      very end, after the other blocks.   */
  421.   struct block *superblock;
  422.   /* A flag indicating whether or not the fucntion corresponding
  423.      to this block was compiled with gcc or not.  If there is no
  424.      function corresponding to this block, this meaning of this flag
  425.      is undefined.  (In practice it will be 1 if the block was created
  426.      while processing a file compiled with gcc and 0 when not). */
  427.   unsigned char gcc_compile_flag;
  428.   /* Number of local symbols.  */
  429.   int nsyms;
  430.   /* The symbols.  */
  431.   struct symbol *sym[1];
  432. };
  433.  
  434. /* Represent one symbol name; a variable, constant, function or typedef.  */
  435.  
  436. /* Different name spaces for symbols.  Looking up a symbol specifies
  437.    a namespace and ignores symbol definitions in other name spaces.
  438.  
  439.    VAR_NAMESPACE is the usual namespace.
  440.    In C, this contains variables, function names, typedef names
  441.    and enum type values.
  442.  
  443.    STRUCT_NAMESPACE is used in C to hold struct, union and enum type names.
  444.    Thus, if `struct foo' is used in a C program,
  445.    it produces a symbol named `foo' in the STRUCT_NAMESPACE.
  446.  
  447.    LABEL_NAMESPACE may be used for names of labels (for gotos);
  448.    currently it is not used and labels are not recorded at all.  */
  449.  
  450. /* For a non-global symbol allocated statically,
  451.    the correct core address cannot be determined by the compiler.
  452.    The compiler puts an index number into the symbol's value field.
  453.    This index number can be matched with the "desc" field of
  454.    an entry in the loader symbol table.  */
  455.  
  456. enum namespace
  457. {
  458.   UNDEF_NAMESPACE, VAR_NAMESPACE, STRUCT_NAMESPACE, LABEL_NAMESPACE
  459. };
  460.  
  461. /* An address-classic says where to find the value of the symbol in core.  */
  462.  
  463. enum address_class
  464. {
  465.   LOC_UNDEF,        /* Not used; catches errors */
  466.   LOC_CONST,        /* Value is constant int */
  467.   LOC_STATIC,        /* Value is at fixed address */
  468.   LOC_REGISTER,        /* Value is in register */
  469.   LOC_ARG,        /* Value is at spec'd position in arglist */
  470.   LOC_REF_ARG,        /* Value address is at spec'd position in */
  471.             /* arglist.  */
  472.   LOC_REGPARM,        /* Value is at spec'd position in  register window */
  473.   LOC_LOCAL,        /* Value is at spec'd pos in stack frame */
  474.   LOC_TYPEDEF,        /* Value not used; definition in SYMBOL_TYPE
  475.                Symbols in the namespace STRUCT_NAMESPACE
  476.                all have this classic.  */
  477.   LOC_LABEL,        /* Value is address in the code */
  478.   LOC_BLOCK,        /* Value is address of a `struct block'.
  479.                Function names have this classic.  */
  480.   LOC_EXTERNAL,        /* Value is at address not in this compilation.
  481.                This is used for .comm symbols
  482.                and for extern symbols within functions.
  483.                Inside GDB, this is changed to LOC_STATIC once the
  484.                real address is obtained from a loader symbol.  */
  485.   LOC_CONST_BYTES    /* Value is a constant byte-sequence.   */
  486. };
  487.  
  488. struct symbol
  489. {
  490.   /* Symbol name */
  491.   char *name;
  492.   /* Name space code.  */
  493.   enum namespace namespace;
  494.   /* Address classic */
  495.   enum address_class classic;
  496.   /* Data type of value */
  497.   struct type *type;
  498.   /* constant value, or address if static, or register number,
  499.      or offset in arguments, or offset in stack frame.  */
  500.   union
  501.     {
  502.       long value;
  503.       struct block *block;      /* for LOC_BLOCK */
  504.       char *bytes;        /* for LOC_CONST_BYTES */
  505.     }
  506.   value;
  507. };
  508.  
  509. struct partial_symbol
  510. {
  511.   /* Symbol name */
  512.   char *name;
  513.   /* Name space code.  */
  514.   enum namespace namespace;
  515.   /* Address classic (for info_symbols) */
  516.   enum address_class classic;
  517.   /* Value (only used for static functions currently).  Done this
  518.      way so that we can use the struct symbol macros.
  519.      Note that the address of a function is SYMBOL_VALUE (pst)
  520.      in a partial symbol table, but BLOCK_START (SYMBOL_BLOCK_VALUE (st))
  521.      in a symbol table.  */
  522.   union
  523.     {
  524.       long value;
  525.     }
  526.   value;
  527. };
  528.  
  529. /* 
  530.  * Vectors of all partial symbols read in from file; actually declared
  531.  * and used in dbxread.c.
  532.  */
  533. extern struct psymbol_allocation_list {
  534.   struct partial_symbol *list, *next;
  535.   int size;
  536. } global_psymbols, static_psymbols;
  537.  
  538.  
  539. /* Source-file information.
  540.    This describes the relation between source files and line numbers
  541.    and addresses in the program text.  */
  542.  
  543. struct sourcevector
  544. {
  545.   int length;            /* Number of source files described */
  546.   struct source *source[1];    /* Descriptions of the files */
  547. };
  548.  
  549. /* Each item represents a line-->pc (or the reverse) mapping.  This is
  550.    somewhat more wasteful of space than one might wish, but since only
  551.    the files which are actually debugged are read in to core, we don't
  552.    waste much space.
  553.  
  554.    Each item used to be an int; either minus a line number, or a
  555.    program counter.  If it represents a line number, that is the line
  556.    described by the next program counter value.  If it is positive, it
  557.    is the program counter at which the code for the next line starts.  */
  558.  
  559. struct linetable_entry
  560. {
  561.   int line;
  562.   CORE_ADDR pc;
  563. };
  564.  
  565. struct linetable
  566. {
  567.   int nitems;
  568.   struct linetable_entry item[1];
  569. };
  570.  
  571. /* All the information on one source file.  */
  572.  
  573. struct source
  574. {
  575.   char *name;            /* Name of file */
  576.   struct linetable contents;
  577. };
  578.  
  579. /*
  580. #ifdef USG
  581. #include <string.h>
  582. #else
  583. #include <strings.h>
  584. #endif
  585. */
  586.  
  587. #define min(a,b) ((a) < (b) ? (a) : (b))
  588.  
  589. /* Macro to control the number of undefined references printed */
  590. #define MAX_UREFS_PRINTED    10
  591.  
  592. /*
  593.  * Ok.  Following are the relocation information macros.  If your
  594.  * system should not be able to use the default set (below), you must
  595.  * define the following:
  596.  
  597.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  598.  * of structure that is stored in the relocation info section of your
  599.  * a.out files.  Often this is defined in the a.out.h for your system.
  600.  *
  601.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  602.  * <whatever> to be relocated.  *Must be an lvalue*.
  603.  *
  604.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  605.  * external ld_symbol (1), or was it fully resolved upon entering the
  606.  * loader (0) in which case some combination of the value in memory
  607.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  608.  * what the value of the relocation actually was.  *Must be an lvalue*.
  609.  *
  610.  *   RELOC_TYPE (rval): If this entry was fully resolved upon
  611.  * entering the loader, what type should it be relocated as?
  612.  *
  613.  *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  614.  * entering the loader, what is the index of it's ld_symbol in the ld_symbol
  615.  * table?  *Must be a lvalue*.
  616.  *
  617.  *   RELOC_MEMORY_ADD_P (rval): This should return true if the final
  618.  * relocation value output here should be added to memory, or if the
  619.  * section of memory described should simply be set to the relocation
  620.  * value.
  621.  *
  622.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  623.  * an extra value to be added to the relocation value based on the
  624.  * individual relocation entry.  *Must be an lvalue if defined*.
  625.  *
  626.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  627.  * pc relative.
  628.  *
  629.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  630.  * final relocation value before putting it where it belongs.
  631.  *
  632.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  633.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  634.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  635.  * do everything in terms of the bit operators below), but having this
  636.  * macro could end up producing better code on machines without fancy
  637.  * bit twiddling.  Also, it's easier to understand/code big/little
  638.  * endian distinctions with this macro.
  639.  *
  640.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  641.  * object described in RELOC_TARGET_SIZE in which the relocation value
  642.  * will go.
  643.  *
  644.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  645.  * with the bits of the relocation value.  It may be assumed by the
  646.  * code that the relocation value will fit into this many bits.  This
  647.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  648.  *
  649.  *
  650.  *        Things I haven't implemented
  651.  *        ----------------------------
  652.  *
  653.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  654.  *
  655.  *    Pc relative relocation for External references.
  656.  *
  657.  *
  658.  */
  659.  
  660. /* The following #if has been modifed for cross compilation */
  661. /* It originally ld_read:  #if defined(sun) && defined(sparc)  */
  662. /* Marc Ullman, Stanford University    Nov. 1 1989  */
  663. #if defined(sun) && (TARGET == SUN4)
  664. /* Sparc (Sun 4) macros */
  665. #undef relocation_info
  666. #define relocation_info                    reloc_info_sparc
  667. #define RELOC_ADDRESS(r)        ((r)->r_address)
  668. #define RELOC_EXTERN_P(r)               ((r)->r_extern)
  669. #define RELOC_TYPE(r)                   ((r)->r_index)
  670. #define RELOC_SYMBOL(r)                 ((r)->r_index)
  671. #define RELOC_MEMORY_SUB_P(r)        0
  672. #define RELOC_MEMORY_ADD_P(r)           0
  673. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)
  674. #define RELOC_PCREL_P(r)             \
  675.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  676. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  677. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  678. #define RELOC_TARGET_BITPOS(r)          0
  679. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  680.  
  681. /* Note that these are very dependent on the order of the enums in
  682.    enum reloc_type (in a.out.h); if they change the following must be
  683.    changed */
  684. /* Also note that the last few may be incorrect; I have no information */
  685. static const int const reloc_target_rightshift[] = {
  686.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  687. };
  688. static const int const reloc_target_size[] = {
  689.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  690. };
  691. static const int const reloc_target_bitsize[] = {
  692.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  693. };
  694.  
  695. #define    MAX_ALIGNMENT    (sizeof (double))
  696. #endif
  697. #ifdef sequent
  698. #define RELOC_ADDRESS(r)        ((r)->r_address)
  699. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  700. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  701. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  702. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  703. #define RELOC_MEMORY_ADD_P(r)    1
  704. #undef RELOC_ADD_EXTRA
  705. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  706. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  707. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  708. #define RELOC_TARGET_BITPOS(r)    0
  709. #define RELOC_TARGET_BITSIZE(r)    32
  710. #endif
  711.  
  712. /* Default macros */
  713. #ifndef RELOC_ADDRESS
  714. #define RELOC_ADDRESS(r)        ((r)->r_address)
  715. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  716. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  717. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  718. #define RELOC_MEMORY_SUB_P(r)    0
  719. #define RELOC_MEMORY_ADD_P(r)    1
  720. #undef RELOC_ADD_EXTRA
  721. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  722. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  723. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  724. #define RELOC_TARGET_BITPOS(r)    0
  725. #define RELOC_TARGET_BITSIZE(r)    32
  726. #endif
  727.  
  728. #ifndef MAX_ALIGNMENT
  729. #define    MAX_ALIGNMENT    (sizeof (int))
  730. #endif
  731.  
  732. #ifdef nounderscore
  733. #define LPREFIX '.'
  734. #else
  735. #define LPREFIX 'L'
  736. #endif
  737.  
  738. #ifndef TEXT_START
  739. #define TEXT_START(x) N_TXTADDR(x)
  740. #endif
  741.  
  742. /* Special global ld_symbol types understood by GNU LD.  */
  743.  
  744. /* The following type indicates the definition of a ld_symbol as being
  745.    an indirect reference to another ld_symbol.  The other ld_symbol
  746.    appears as an undefined reference, immediately following this ld_symbol.
  747.  
  748.    Indirection is asymmetrical.  The other ld_symbol's value will be used
  749.    to satisfy requests for the indirect ld_symbol, but not vice versa.
  750.    If the other ld_symbol does not have a definition, libraries will
  751.    be searched to find a definition.
  752.  
  753.    So, for example, the following two lines placed in an assembler
  754.    input file would result in an object file which would direct gnu ld
  755.    to resolve all references to ld_symbol "foo" as references to ld_symbol
  756.    "bar".
  757.  
  758.     .stabs "_foo",11,0,0,0
  759.     .stabs "_bar",1,0,0,0
  760.  
  761.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  762.  
  763. #ifndef N_INDR
  764. #define N_INDR 0xa
  765. #endif
  766.  
  767. /* The following symbols refer to set elements.  These are expected
  768.    only in input to the loader; they should not appear in loader
  769.    output (unless relocatable output is requested).  To be recognized
  770.    by the loader, the input symbols must have their N_EXT bit set.
  771.    All the N_SET[ATDB] symbols with the same name form one set.  The
  772.    loader collects all of these elements at load time and outputs a
  773.    vector for each name.
  774.    Space (an array of 32 bit words) is allocated for the set in the
  775.    data section, and the n_value field of each set element value is
  776.    stored into one word of the array.
  777.    The first word of the array is the length of the set (number of
  778.    elements).  The last word of the vector is set to zero for possible
  779.    use by incremental loaders.  The array is ordered by the linkage
  780.    order; the first symbols which the linker encounters will be first
  781.    in the array.
  782.  
  783.    In C syntax this looks like:
  784.  
  785.     struct set_vector {
  786.       unsigned int length;
  787.       unsigned int vector[length];
  788.       unsigned int always_zero;
  789.     };
  790.  
  791.    Before being placed into the array, each element is relocated
  792.    according to its type.  This allows the loader to create an array
  793.    of pointers to objects automatically.  N_SETA type symbols will not
  794.    be relocated.
  795.  
  796.    The address of the set is made into an N_SETV ld_symbol
  797.    whose name is the same as the name of the set.
  798.    This ld_symbol acts like a N_DATA global ld_symbol
  799.    in that it can satisfy undefined external references.
  800.  
  801.    For the purposes of determining whether or not to load in a library
  802.    file, set element definitions are not considered "real
  803.    definitions"; they will not cause the loading of a library
  804.    member.
  805.  
  806.    If relocatable output is requested, none of this processing is
  807.    done.  The symbols are simply relocated and passed through to the
  808.    output file.
  809.  
  810.    So, for example, the following three lines of assembler code
  811.    (whether in one file or scattered between several different ones)
  812.    will produce a three element vector (total length is five words;
  813.    see above), referenced by the ld_symbol "_xyzzy", which will have the
  814.    addresses of the routines _init1, _init2, and _init3.
  815.  
  816.    *NOTE*: If symbolic addresses are used in the n_value field of the
  817.    defining .stabs, those symbols must be defined in the same file as
  818.    that containing the .stabs.
  819.  
  820.     .stabs "_xyzzy",23,0,0,_init1
  821.     .stabs "_xyzzy",23,0,0,_init2
  822.     .stabs "_xyzzy",23,0,0,_init3
  823.  
  824.    Note that (23 == (N_SETT | N_EXT)).  */
  825.  
  826. #ifndef N_SETA
  827. #define    N_SETA    0x14        /* Absolute set element ld_symbol */
  828. #endif                /* This is input to LD, in a .o file.  */
  829.  
  830. #ifndef N_SETT
  831. #define    N_SETT    0x16        /* Text set element ld_symbol */
  832. #endif                /* This is input to LD, in a .o file.  */
  833.  
  834. #ifndef N_SETD
  835. #define    N_SETD    0x18        /* Data set element ld_symbol */
  836. #endif                /* This is input to LD, in a .o file.  */
  837.  
  838. #ifndef N_SETB
  839. #define    N_SETB    0x1A        /* Bss set element ld_symbol */
  840. #endif                /* This is input to LD, in a .o file.  */
  841.  
  842. /* Macros dealing with the set element symbols defined in a.out.h */
  843. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  844. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  845.  
  846. #ifndef N_SETV
  847. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  848. #endif                /* This is output from LD.  */
  849.  
  850. /* If a this type of ld_symbol is encountered, its name is a warning
  851.    message to print each time the ld_symbol referenced by the next ld_symbol
  852.    table entry is referenced.
  853.  
  854.    This feature may be used to allow backwards compatibility with
  855.    certain functions (eg. gets) but to discourage programmers from
  856.    their use.
  857.  
  858.    So if, for example, you wanted to have ld print a warning whenever
  859.    the function "gets" was used in their C program, you would add the
  860.    following to the assembler file in which gets is defined:
  861.  
  862.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  863.     .stabs "_gets",1,0,0,0
  864.  
  865.    These .stabs do not necessarily have to be in the same file as the
  866.    gets function, they simply must exist somewhere in the compilation.  */
  867.  
  868. #ifndef N_WARNING
  869. #define N_WARNING 0x1E        /* Warning message to print if ld_symbol
  870.                    included */
  871. #endif                /* This is input to ld */
  872.  
  873. #ifndef __GNU_STAB__
  874.  
  875. /* Line number for the data section.  This is to be used to describe
  876.    the source location of a variable declaration.  */
  877. #ifndef N_DSLINE
  878. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  879. #endif
  880.  
  881. /* Line number for the bss section.  This is to be used to describe
  882.    the source location of a variable declaration.  */
  883. #ifndef N_BSLINE
  884. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  885. #endif
  886.  
  887. #endif /* not __GNU_STAB__ */
  888.  
  889. /* Symbol table */
  890.  
  891. /* Global ld_symbol data is recorded in these structures,
  892.    one for each global ld_symbol.
  893.    They are found via hashing in 'symtab', which points to a vector of buckets.
  894.    Each bucket is a chain of these structures through the link field.  */
  895.  
  896. typedef
  897.   struct glosym
  898.     {
  899.       /* Pointer to next ld_symbol in this ld_symbol's hash bucket.  */
  900.       struct glosym *link;
  901.       /* Name of this ld_symbol.  */
  902.       char *name;
  903.       /* Value of this ld_symbol as a global ld_symbol.  */
  904.       long value;
  905.       /* Chain of external 'nlist's in files for this ld_symbol, both defs
  906.      and refs.  */
  907.       struct nlist *refs;
  908.       /* Any warning message that might be associated with this ld_symbol
  909.          from an N_WARNING ld_symbol encountered. */
  910.       char *warning;
  911.       /* Nonzero means definitions of this ld_symbol as common have been seen,
  912.      and the value here is the largest size specified by any of them.  */
  913.       int max_common_size;
  914.       /* For relocatable_output, records the index of this global sym in the
  915.      ld_symbol table to be written, with the first global sym given index 0.*/
  916.       int def_count;
  917.       /* Nonzero means a definition of this global ld_symbol is known to exist.
  918.      Library members should not be loaded on its account.  */
  919.       char defined;
  920.       /* Nonzero means a reference to this global ld_symbol has been seen
  921.      in a file that is surely being loaded.
  922.      A value higher than 1 is the n_type code for the ld_symbol's
  923.      definition.  */
  924.       char referenced;
  925.       /* A count of the number of undefined references printed for a
  926.      specific ld_symbol.  If a ld_symbol is unresolved at the end of
  927.      digest_symbols (and the loading run is supposed to produce
  928.      relocatable output) do_file_warnings keeps track of how many
  929.      unresolved reference error messages have been printed for
  930.      each ld_symbol here.  When the number hits MAX_UREFS_PRINTED,
  931.      messages stop. */
  932.       unsigned char undef_refs;
  933.       /* 1 means that this ld_symbol has multiple definitions.  2 means
  934.          that it has multiple definitions, and some of them are set
  935.      elements, one of which has been printed out already.  */
  936.       unsigned char multiply_defined;
  937.       /* Nonzero means print a message at all refs or defs of this ld_symbol */
  938.       char trace;
  939.     }
  940.   ld_symbol;
  941.  
  942. /* Demangler for C++. */
  943. extern char *cplus_demangle ();
  944.  
  945. /* Demangler function to use. */
  946. char *(*demangler)() = NULL;
  947.  
  948. /* Number of buckets in ld_symbol hash table */
  949. #define    TABSIZE    1009
  950.  
  951. /* The ld_symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  952. ld_symbol *symtab[TABSIZE];
  953.  
  954. /* Number of symbols in ld_symbol hash table. */
  955. int num_hash_tab_syms = 0;
  956.  
  957. /* Count the number of nlist entries that are for local symbols.
  958.    This count and the three following counts
  959.    are incremented as as symbols are entered in the ld_symbol table.  */
  960. int local_sym_count;
  961.  
  962. /* Count number of nlist entries that are for local symbols
  963.    whose names don't start with L. */
  964. int non_L_local_sym_count;
  965.  
  966. /* Count the number of nlist entries for debugger info.  */
  967. int debugger_sym_count;
  968.  
  969. /* Count the number of global symbols referenced and not defined.  */
  970. int undefined_global_sym_count;
  971.  
  972. /* Count the number of global symbols multiply defined.  */
  973. int multiple_def_count;
  974.  
  975. /* Count the number of defined global symbols.
  976.    Each ld_symbol is counted only once
  977.    regardless of how many different nlist entries refer to it,
  978.    since the output file will need only one nlist entry for it.
  979.    This count is computed by `digest_symbols';
  980.    it is undefined while symbols are being loaded. */
  981. int defined_global_sym_count;
  982.  
  983. /* Count the number of symbols defined through common declarations.
  984.    This count is kept in symdef_library, linear_library, and
  985.    enter_global_ref.  It is incremented when the defined flag is set
  986.    in a ld_symbol because of a common definition, and decremented when
  987.    the ld_symbol is defined "for real" (ie. by something besides a common
  988.    definition).  */
  989. int common_defined_global_count;
  990.  
  991. /* Count the number of set element type symbols and the number of
  992.    separate vectors which these symbols will fit into.  See the
  993.    GNU a.out.h for more info.
  994.    This count is computed by 'enter_file_symbols' */
  995. int set_symbol_count;
  996. int set_vector_count;
  997.  
  998. /* Define a linked list of strings which define symbols which should
  999.    be treated as set elements even though they aren't.  Any ld_symbol
  1000.    with a prefix matching one of these should be treated as a set
  1001.    element.
  1002.  
  1003.    This is to make up for deficiencies in many assemblers which aren't
  1004.    willing to pass any stabs through to the loader which they don't
  1005.    understand.  */
  1006. struct string_list_element {
  1007.   char *str;
  1008.   struct string_list_element *next;
  1009. };
  1010.  
  1011. struct string_list_element *set_element_prefixes;
  1012.  
  1013. /* Count the number of definitions done indirectly (ie. done relative
  1014.    to the value of some other ld_symbol. */
  1015. int global_indirect_count;
  1016.  
  1017. /* Count the number of warning symbols encountered. */
  1018. int warning_count;
  1019.  
  1020. /* Total number of symbols to be written in the output file.
  1021.    Computed by digest_symbols from the variables above.  */
  1022. int nsyms;
  1023.  
  1024.  
  1025. /* Nonzero means ptr to ld_symbol entry for ld_symbol to use as start addr.
  1026.    -e sets this.  */
  1027. ld_symbol *edata_symbol;   /* the ld_symbol _edata */
  1028. ld_symbol *etext_symbol;   /* the ld_symbol _etext */
  1029. ld_symbol *end_symbol;    /* the ld_symbol _end */
  1030. ld_symbol *big_stack;    /* the ld_symbol _end */
  1031.  
  1032. /* Each input file, and each library member ("subfile") being loaded,
  1033.    has a `file_entry' structure for it.
  1034.  
  1035.    For files specified by command args, these are contained in the vector
  1036.    which `file_table' points to.
  1037.  
  1038.    For library members, they are dynamically allocated,
  1039.    and chained through the `chain' field.
  1040.    The chain is found in the `subfiles' field of the `file_entry'.
  1041.    The `file_entry' objects for the members have `superfile' fields pointing
  1042.    to the one for the library.  */
  1043.  
  1044. struct file_entry {
  1045.   /* Name of this file.  */
  1046.   char *filename;
  1047.   /* Name to use for the ld_symbol giving address of text start */
  1048.   /* Usually the same as filename, but for a file spec'd with -l
  1049.      this is the -l switch itself rather than the filename.  */
  1050.   char *local_sym_name;
  1051.  
  1052.   /* Describe the layout of the contents of the file */
  1053.  
  1054.   /* The file's a.out header.  */
  1055.   struct exec header;
  1056.   /* Offset in file of GDB ld_symbol segment, or 0 if there is none.  */
  1057.   int symseg_offset;
  1058.  
  1059.   /* Describe data from the file loaded into core */
  1060.  
  1061.   /* Symbol table of the file.  */
  1062.   struct nlist *symbols;
  1063.   /* Size in bytes of string table.  */
  1064.   int string_size;
  1065.   /* Pointer to the string table.
  1066.      The string table is not kept in core all the time,
  1067.      but when it is in core, its address is here.  */
  1068.   char *strings;
  1069.  
  1070.   /* Next two used only if `relocatable_output' or if needed for */
  1071.   /* output of undefined reference line numbers. */
  1072.  
  1073.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  1074.   struct relocation_info *textrel;
  1075.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  1076.   struct relocation_info *datarel;
  1077.  
  1078.   /* Relation of this file's segments to the output file */
  1079.  
  1080.   /* Start of this file's text seg in the output file core image.  */
  1081.   int text_start_address;
  1082.   /* Start of this file's data seg in the output file core image.  */
  1083.   int data_start_address;
  1084.   /* Start of this file's bss seg in the output file core image.  */
  1085.   int bss_start_address;
  1086.   /* Offset in bytes in the output file ld_symbol table
  1087.      of the first local ld_symbol for this file.  Set by `write_file_symbols'.  */
  1088.   int local_syms_offset;
  1089.  
  1090.   /* For library members only */
  1091.  
  1092.   /* For a library, points to chain of entries for the library members.  */
  1093.   struct file_entry *subfiles;
  1094.   /* For a library member, offset of the member within the archive.
  1095.      Zero for files that are not library members.  */
  1096.   int starting_offset;
  1097.   /* Size of contents of this file, if library member.  */
  1098.   int total_size;
  1099.   /* For library member, points to the library's own entry.  */
  1100.   struct file_entry *superfile;
  1101.   /* For library member, points to next entry for next member.  */
  1102.   struct file_entry *chain;
  1103.  
  1104.   /* 1 if file is a library. */
  1105.   char library_flag;
  1106.  
  1107.   /* 1 if file's header has been ld_read into this structure.  */
  1108.   char header_read_flag;
  1109.  
  1110.   /* 1 means search a set of directories for this file.  */
  1111.   char search_dirs_flag;
  1112.  
  1113.   /* 1 means this is base file of incremental load.
  1114.      Do not load this file's text or data.
  1115.      Also default text_start to after this file's bss. */
  1116.   char just_syms_flag;
  1117. };
  1118.  
  1119. /* Vector of entries for input files specified by arguments.
  1120.    These are all the input files except for members of specified libraries.  */
  1121. struct file_entry *file_table;
  1122.  
  1123. /* When loading the text and data, we can avoid doing a close
  1124.    and another open between members of the same library.
  1125.  
  1126.    These two variables remember the file that is currently open.
  1127.    Both are zero if no file is open.
  1128.  
  1129.    See `each_file' and `file_close'.  */
  1130.  
  1131. struct file_entry *input_file;
  1132. int input_desc;
  1133.  
  1134. /* The following are computed by `digest_symbols'.  */
  1135.  
  1136. int text_size;        /* total size of text of all input files.  */
  1137. int data_size;        /* total size of data of all input files.  */
  1138. int bss_size;        /* total size of bss of all input files.  */
  1139. int text_reloc_size;    /* total size of text relocation of all input files.  */
  1140. int data_reloc_size;    /* total size of data relocation of all input */
  1141.             /* files.  */
  1142.  
  1143. /* Specifications of start and length of the area reserved at the end
  1144.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  1145. int set_sect_start;
  1146. int set_sect_size;
  1147.  
  1148. /* Amount of cleared space to leave between the text and data segments.  */
  1149.  
  1150. int text_pad;
  1151.  
  1152. /* Amount of bss segment to include as part of the data segment.  */
  1153.  
  1154. int data_pad;
  1155.  
  1156. /* Format of __.SYMDEF:
  1157.    First, a longword containing the size of the 'symdef' data that follows.
  1158.    Second, zero or more 'symdef' structures.
  1159.    Third, a longword containing the length of ld_symbol name strings.
  1160.    Fourth, zero or more ld_symbol name strings (each followed by a null).  */
  1161.  
  1162. struct symdef {
  1163.   int symbol_name_string_index;
  1164.   int library_member_offset;
  1165. };
  1166.  
  1167. /* Record most of the command options.  */
  1168.  
  1169. /* Address we assume the text section will be loaded at.
  1170.    We relocate symbols and text and data for this, but we do not
  1171.    write any padding in the output file for it.  */
  1172. int text_start;
  1173.  
  1174. /* Offset of default entry-pc within the text section.  */
  1175. int entry_offset;
  1176.  
  1177. /* Address we decide the data section will be loaded at.  */
  1178. int data_start;
  1179.  
  1180. /* Size to pad data section up to.
  1181.    We simply increase the size of the data section, padding with zeros,
  1182.    and reduce the size of the bss section to match.  */
  1183. int specified_data_size;
  1184.  
  1185. /* Magic number to use for the output file, set by switch.  */
  1186. int magic;
  1187.  
  1188. /* Which symbols should be stripped (omitted from the output):
  1189.    none, all, or debugger symbols.  */
  1190. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  1191.  
  1192. /* Which local symbols should be omitted:
  1193.    none, all, or those starting with L.
  1194.    This is irrelevant if STRIP_NONE.  */
  1195. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  1196.  
  1197. /* Non zero means to create the output executable. */
  1198. /* Cleared by nonfatal errors.  */
  1199. int make_executable;
  1200.  
  1201. /* Force the executable to be output, even if there are non-fatal
  1202.    errors */
  1203. int force_executable;
  1204.  
  1205. void bcopy (), bzero ();
  1206.  
  1207. void error ();
  1208.  
  1209. int digest_symbols ();
  1210. void print_symbols ();
  1211. void load_symbols ();
  1212. void decode_command ();
  1213. void list_undefined_symbols ();
  1214. void list_unresolved_references ();
  1215. void write_output ();
  1216. void write_header ();
  1217. void write_text ();
  1218. void write_data ();
  1219. void write_rel ();
  1220. void write_syms ();
  1221. void write_symsegs ();
  1222. void mywrite ();
  1223. void symtab_init ();
  1224. void padfile ();
  1225. char *get_file_name ();
  1226. ld_symbol *getsym (), *getsym_soft ();
  1227.  
  1228. char *resource_text;
  1229.  
  1230. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1231. void enter_file_symbols (), enter_global_ref (), search_library ();
  1232. void modify_location (), copy_text (), copy_data ();
  1233. int perform_relocation (
  1234.      char *data,
  1235.      int pc_relocation,
  1236.      int data_size,
  1237.      struct relocation_info *reloc_info,
  1238.      int reloc_size,
  1239.      struct file_entry *entry);
  1240.  
  1241. /* Offsets and current lengths of ld_symbol and string tables in output file. */
  1242.  
  1243. int symbol_table_offset;
  1244. int symbol_table_len;
  1245.  
  1246. /* Address in output file where string table starts.  */
  1247. int string_table_offset;
  1248.  
  1249. /* Offset within string table
  1250.    where the strings in `strtab_vector' should be written.  */
  1251. int string_table_len;
  1252.  
  1253. /* Total size of string table strings allocated so far,
  1254.    including strings in `strtab_vector'.  */
  1255. int strtab_size;
  1256.  
  1257. /* Vector whose elements are strings to be added to the string table.  */
  1258. char **strtab_vector;
  1259.  
  1260. /* Vector whose elements are the lengths of those strings.  */
  1261. int *strtab_lens;
  1262.  
  1263. /* Index in `strtab_vector' at which the next string will be stored.  */
  1264. int strtab_index;
  1265.  
  1266. char *xmalloc();
  1267.  
  1268. void *errno_addr;
  1269.  
  1270. int loader_main (int desc, struct exec *hdr, int resource_size)
  1271. {
  1272.   register char *bytes;
  1273.   struct file_entry fe;
  1274.   register int len,coresize;
  1275.   register int i;
  1276.  
  1277. #if 1
  1278. errno_addr = 0;
  1279. bss_size = 0;
  1280. common_defined_global_count = 0;
  1281. data_pad = 0;
  1282. data_reloc_size = 0;
  1283. data_size = 0;
  1284. data_start = 0;
  1285. debugger_sym_count = 0;
  1286. defined_global_sym_count = 0;
  1287. demangler = 0;
  1288. discard_locals = 0;
  1289. edata_symbol = 0;
  1290. end_symbol = 0;
  1291. entry_offset = 0;
  1292. etext_symbol = 0;
  1293. file_table = 0;
  1294. force_executable = 0;
  1295. global_indirect_count = 0;
  1296. input_desc = 0;
  1297. input_file = 0;
  1298. local_sym_count = 0;
  1299. magic = 0;
  1300. make_executable = 0;
  1301. multiple_def_count = 0;
  1302. non_L_local_sym_count = 0;
  1303. nsyms = 0;
  1304. num_hash_tab_syms = 0;
  1305. resource_text = 0;
  1306. set_element_prefixes = 0;
  1307. set_sect_size = 0;
  1308. set_sect_start = 0;
  1309. set_symbol_count = 0;
  1310. set_vector_count = 0;
  1311. specified_data_size = 0;
  1312. string_table_len = 0;
  1313. string_table_offset = 0;
  1314. strip_symbols = 0;
  1315. strtab_index = 0;
  1316. strtab_lens = 0;
  1317. strtab_size = 0;
  1318. strtab_vector = 0;
  1319. symbol_table_len = 0;
  1320. symbol_table_offset = 0;
  1321. bzero(symtab, sizeof(symtab));
  1322. text_pad = 0;
  1323. text_reloc_size = 0;
  1324. text_size = 0;
  1325. text_start = 0;
  1326. undefined_global_sym_count = 0;
  1327. warning_count = 0;
  1328. #endif
  1329.   
  1330.   /* Clear the cumulative info on the output file.  */
  1331.  
  1332.   text_size = 0;
  1333.   data_size = 0;
  1334.   bss_size = 0;
  1335.   text_reloc_size = 0;
  1336.   data_reloc_size = 0;
  1337.  
  1338.   data_pad = 0;
  1339.   text_pad = 0;
  1340.  
  1341.   /* Initialize the data about options.  */
  1342.  
  1343.   specified_data_size = 0;
  1344.   strip_symbols = STRIP_NONE;
  1345.   discard_locals = DISCARD_NONE;
  1346.   magic = OMAGIC;
  1347.   make_executable = 1;
  1348.   force_executable = 0;
  1349.   set_element_prefixes = 0;
  1350.  
  1351.   /* Initialize the cumulative counts of symbols.  */
  1352.  
  1353.   local_sym_count = 0;
  1354.   non_L_local_sym_count = 0;
  1355.   debugger_sym_count = 0;
  1356.   undefined_global_sym_count = 0;
  1357.   set_symbol_count = 0;
  1358.   set_vector_count = 0;
  1359.   global_indirect_count = 0;
  1360.   warning_count = 0;
  1361.   multiple_def_count = 0;
  1362.   common_defined_global_count = 0;
  1363.  
  1364.   file_table = &fe;
  1365.   bzero (file_table, sizeof (struct file_entry));
  1366.  
  1367.   file_table->filename = "a.out";
  1368.   file_table->local_sym_name = "a.out";
  1369.  
  1370.     memcpy(&file_table->header, hdr, sizeof (struct exec));
  1371.  
  1372.     coresize = N_DATOFF(file_table->header) + 
  1373.             (file_table->header).a_data + 
  1374.             (file_table->header).a_bss;
  1375.  
  1376.     if (coresize > resource_size) resource_size = coresize;
  1377.  
  1378.     resource_text = (char *)emptyblock(resource_size);
  1379.  
  1380.     read(desc, ((char *) (resource_text)) + sizeof(struct exec), resource_size);
  1381.         
  1382.   /* Create the symbols `etext', `edata' and `end'.  */
  1383.  
  1384.     symtab_init ();
  1385.             
  1386.     text_start = sizeof(struct exec) + ((long) (resource_text));
  1387.     data_start = N_DATOFF(file_table->header) + ((long) (resource_text));
  1388.  
  1389.   file_table->header_read_flag = 1;
  1390.   file_table->symbols = (struct nlist *)(N_SYMOFF (file_table->header)  + (char *)(resource_text));
  1391.   file_table->string_size = *(int *)( N_STROFF (file_table->header) + (char *)(resource_text));
  1392.   file_table->strings = N_STROFF (file_table->header) + (char *)(resource_text);
  1393.   file_table->textrel = (struct relocation_info *)(text_offset (file_table) + file_table->header.a_text + file_table->header.a_data + (char *)(resource_text));
  1394.   file_table->datarel = (struct relocation_info *)(text_offset (file_table) + file_table->header.a_text + file_table->header.a_data + file_table->header.a_trsize + (char *)(resource_text));
  1395.   enter_file_symbols (file_table);
  1396.   file_table->text_start_address = text_size;
  1397.   text_size += file_table->header.a_text;
  1398.   file_table->data_start_address = data_size;
  1399.   data_size += file_table->header.a_data;
  1400.   file_table->bss_start_address = bss_size;
  1401.   bss_size += file_table->header.a_bss;
  1402.   text_reloc_size += file_table->header.a_trsize;
  1403.   data_reloc_size += file_table->header.a_drsize;
  1404.  
  1405.   if (digest_symbols ())
  1406.       {
  1407.       fatal("relocatable module not linked with -d\n");
  1408.       return -1;
  1409.       }
  1410.  
  1411.   /* Print error messages for any missing symbols, for any warning
  1412.      symbols, and possibly multiple definitions */
  1413.  
  1414.   bytes = text_offset (file_table) + (char *)resource_text;
  1415.  
  1416.   if (perform_relocation (bytes, file_table->text_start_address, file_table->header.a_text,
  1417.               file_table->textrel, file_table->header.a_trsize, file_table))
  1418.         return -1;
  1419.  
  1420.   bytes = text_offset (file_table) + file_table->header.a_text + (char *)resource_text;
  1421.  
  1422.   if (perform_relocation (bytes, file_table->data_start_address - file_table->header.a_text,
  1423.               file_table->header.a_data, file_table->datarel, file_table->header.a_drsize, file_table))
  1424.         return -1;
  1425.               
  1426.   bzero((void *)file_table->bss_start_address, (file_table->header).a_bss);
  1427.  
  1428.  
  1429.    big_stack = getsym_soft("_start");
  1430.   if (big_stack && big_stack->defined && make_executable) hdr->a_entry = (big_stack->value);
  1431.   else resource_size = -1;
  1432.  
  1433.          {
  1434.           int hashval;
  1435.           long type;
  1436.           for (hashval = TABSIZE; hashval--; )
  1437.               {
  1438.               ld_symbol *bp;
  1439.               for (bp = symtab[hashval]; bp; )
  1440.                 {
  1441.                 ld_symbol *nxt = bp->link;
  1442.                   if (!bp->defined || bp->defined == (N_UNDF | N_EXT))
  1443.                     return fatal (bp->name);
  1444.                 free(bp->name);
  1445.                 free(bp);
  1446.                 bp = nxt;
  1447.                 }
  1448.             }
  1449.         }
  1450. SetPtrSize(resource_text,
  1451.     N_DATOFF(file_table->header) + 
  1452.     (file_table->header).a_data + 
  1453.     (file_table->header).a_bss);
  1454.  return resource_size;
  1455. }
  1456.  
  1457.  
  1458. int
  1459. set_element_prefixed_p (name)
  1460.      char *name;
  1461. {
  1462.   struct string_list_element *p;
  1463.   int i;
  1464.  
  1465.   for (p = set_element_prefixes; p; p = p->next)
  1466.     {
  1467.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1468.     ;
  1469.  
  1470.       if (p->str[i] == '\0')
  1471.     return 1;
  1472.     }
  1473.   return 0;
  1474. }
  1475.  
  1476. /* Record an option and arrange to act on it later.
  1477.    ARG should be the following command argument,
  1478.    which may or may not be used by this option.
  1479.  
  1480.    The `l' and `A' options are ignored here since they actually
  1481.    specify input files.  */
  1482.  
  1483.  
  1484. /** Convenient functions for operating on one or all files being */
  1485.  /** loaded.  */
  1486. void print_file_name ();
  1487.  
  1488.  
  1489. /* Close the input file that is now open.  */
  1490.  
  1491. void
  1492. file_close ()
  1493. {
  1494.   input_file = 0;
  1495. }
  1496.  
  1497. /* Open the input file specified by 'entry', and return a descriptor.
  1498.    The open file is remembered; if the same file is opened twice in a row,
  1499.    a new open is not actually done.  */
  1500.  
  1501. int
  1502. file_open (entry)
  1503.      register struct file_entry *entry;
  1504. {
  1505.     return input_desc;
  1506. }
  1507.  
  1508.  
  1509. /* Medium-level input routines for rel files.  */
  1510.  
  1511. /* Read a file's header into the proper place in the file_entry.
  1512.    DESC is the descriptor on which the file is open.
  1513.    ENTRY is the file's entry.  */
  1514. /* Read the string table of file ENTRY into core.
  1515.    Assume it is already open, on descriptor DESC.
  1516.    Also record whether a GDB ld_symbol segment follows the string table.  */
  1517.  
  1518.  
  1519. /* Enter the external ld_symbol defs and refs of ENTRY in the hash table.  */
  1520.  
  1521. void
  1522. enter_file_symbols (entry)
  1523.      struct file_entry *entry;
  1524. {
  1525.   register struct nlist
  1526.     *p,
  1527.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1528.  
  1529.   for (p = entry->symbols; p < end; p++)
  1530.     {
  1531.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1532.       if (set_element_prefixes
  1533.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1534.     p->n_type += (N_SETA - N_ABS);
  1535.  
  1536.       if (SET_ELEMENT_P (p->n_type))
  1537.     {
  1538.       set_symbol_count++;
  1539.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1540.     }
  1541.       else if (p->n_type == N_WARNING)
  1542.     {
  1543.       char *name = p->n_un.n_strx + entry->strings;
  1544.  
  1545.       /* Grab the next entry.  */
  1546.       p++;
  1547.       if (p->n_type != (N_UNDF | N_EXT))
  1548.         {
  1549.           make_executable = 0;
  1550.           p--;        /* Process normally.  */
  1551.         }
  1552.       else
  1553.         {
  1554.           ld_symbol *sp;
  1555.           char *sname = p->n_un.n_strx + entry->strings;
  1556.           /* Deal with the warning ld_symbol.  */
  1557.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1558.           sp = getsym (sname);
  1559.           sp->warning = name;
  1560.           warning_count++;
  1561.         }
  1562.     }
  1563.       else if (p->n_type & N_EXT)
  1564.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1565.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1566.     {
  1567.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1568.         non_L_local_sym_count++;
  1569.       local_sym_count++;
  1570.     }
  1571.       else debugger_sym_count++;
  1572.     }
  1573.  
  1574.    /* Count one for the local ld_symbol that we generate,
  1575.       whose name is the file's name (usually) and whose address
  1576.       is the start of the file's text.  */
  1577.  
  1578.   local_sym_count++;
  1579.   non_L_local_sym_count++;
  1580. }
  1581.  
  1582. /* Enter one global ld_symbol in the hash table.
  1583.    NLIST_P points to the `struct nlist' ld_read from the file
  1584.    that describes the global ld_symbol.  NAME is the ld_symbol's name.
  1585.    ENTRY is the file entry for the file the ld_symbol comes from.
  1586.  
  1587.    The `struct nlist' is modified by placing it on a chain of
  1588.    all such structs that refer to the same global ld_symbol.
  1589.    This chain starts in the `refs' field of the ld_symbol table entry
  1590.    and is chained through the `n_name'.  */
  1591.  
  1592. void
  1593. enter_global_ref (nlist_p, name, entry)
  1594.      register struct nlist *nlist_p;
  1595.      char *name;
  1596.      struct file_entry *entry;
  1597. {
  1598.   register ld_symbol *sp = getsym (name);
  1599.   register int type = nlist_p->n_type;
  1600.   int oldref = sp->referenced;
  1601.   int olddef = sp->defined;
  1602.  
  1603.   nlist_p->n_un.n_name = (char *) sp->refs;
  1604.   sp->refs = nlist_p;
  1605.  
  1606.   sp->referenced = 1;
  1607.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1608.     {
  1609.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1610.     sp->defined = type;
  1611.  
  1612.       if (oldref && !olddef)
  1613.     /* It used to be undefined and we're defining it.  */
  1614.     undefined_global_sym_count--;
  1615.  
  1616.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1617.     {
  1618.       /* First definition and it's common.  */
  1619.       common_defined_global_count++;
  1620.       sp->max_common_size = nlist_p->n_value;
  1621.     }
  1622.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1623.     {
  1624.       /* It used to be common and we're defining it as
  1625.          something else.  */
  1626.       common_defined_global_count--;
  1627.       sp->max_common_size = 0;
  1628.     }
  1629.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1630.       && sp->max_common_size < nlist_p->n_value)
  1631.     /* It used to be common and this is a new common entry to
  1632.        which we need to pay attention.  */
  1633.     sp->max_common_size = nlist_p->n_value;
  1634.  
  1635.       /* Are we defining it as a set element?  */
  1636.       if (SET_ELEMENT_P (type)
  1637.       && (!olddef || (olddef && sp->max_common_size)))
  1638.     set_vector_count++;
  1639.       /* As an indirection?  */
  1640.       else if (type == (N_INDR | N_EXT))
  1641.     {
  1642.       /* Indirect symbols value should be modified to point
  1643.          a ld_symbol being equivalenced to. */
  1644.       nlist_p->n_value
  1645.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1646.                      + entry->strings);
  1647.       if ((ld_symbol *) nlist_p->n_value == sp)
  1648.         {
  1649.           /* Rewrite this ld_symbol as being a global text ld_symbol
  1650.          with value 0.  */
  1651.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  1652.           nlist_p->n_value = 0;
  1653.           /* Don't make the output executable.  */
  1654.           make_executable = 0;
  1655.         }
  1656.       else
  1657.         global_indirect_count++;
  1658.     }
  1659.     }
  1660.   else
  1661.     if (!oldref)
  1662.       undefined_global_sym_count++;
  1663.  
  1664.   if (sp->trace)
  1665.     {
  1666.       register char *reftype;
  1667.       switch (type & N_TYPE)
  1668.     {
  1669.     case N_UNDF:
  1670.       if (nlist_p->n_value)
  1671.         reftype = "defined as common";
  1672.       else reftype = "referenced";
  1673.       break;
  1674.  
  1675.     case N_ABS:
  1676.       reftype = "defined as absolute";
  1677.       break;
  1678.  
  1679.     case N_TEXT:
  1680.       reftype = "defined in text section";
  1681.       break;
  1682.  
  1683.     case N_DATA:
  1684.       reftype = "defined in data section";
  1685.       break;
  1686.  
  1687.     case N_BSS:
  1688.       reftype = "defined in BSS section";
  1689.       break;
  1690.  
  1691.     case N_SETT:
  1692.       reftype = "is a text set element";
  1693.       break;
  1694.  
  1695.     case N_SETD:
  1696.       reftype = "is a data set element";
  1697.       break;
  1698.  
  1699.     case N_SETB:
  1700.       reftype = "is a BSS set element";
  1701.       break;
  1702.  
  1703.     case N_SETA:
  1704.       reftype = "is an absolute set element";
  1705.       break;
  1706.  
  1707.     case N_SETV:
  1708.       reftype = "defined in data section as vector";
  1709.       break;
  1710.  
  1711.     case N_INDR:
  1712.       reftype = "defined equivalent";
  1713.       break;
  1714.  
  1715.     }
  1716.  
  1717.     }
  1718. }
  1719.  
  1720. /* This return 0 if the given file entry's ld_symbol table does *not*
  1721.    contain the nlist point entry, and it returns the files entry
  1722.    pointer (cast to unsigned long) if it does. */
  1723.  
  1724. unsigned long
  1725. contains_symbol (entry, n_ptr)
  1726.      struct file_entry *entry;
  1727.      register struct nlist *n_ptr;
  1728. {
  1729.   if (n_ptr >= entry->symbols &&
  1730.       n_ptr < (entry->symbols
  1731.            + (entry->header.a_syms / sizeof (struct nlist))))
  1732.     return (unsigned long) entry;
  1733.   return 0;
  1734. }
  1735.  
  1736.  
  1737.  
  1738.  
  1739. void consider_file_section_lengths ();
  1740. void relocate_file_addresses (register struct file_entry *entry);
  1741.  
  1742. /* Having entered all the global symbols and found the sizes of sections
  1743.    of all files to be linked, make all appropriate deductions from this data.
  1744.  
  1745.    We propagate global ld_symbol values from definitions to references.
  1746.    We compute the layout of the output file and where each input file's
  1747.    contents fit into it.  */
  1748.  
  1749. int
  1750. digest_symbols ()
  1751. {
  1752.   register int i;
  1753.   int setv_fill_count;
  1754.  
  1755.   /* Set up the set element vector */
  1756.  
  1757.     {
  1758.       /* The set sector size is the number of set elements + a word
  1759.          for each ld_symbol for the length word at the beginning of the
  1760.      vector, plus a word for each ld_symbol for a zero at the end of
  1761.      the vector (for incremental linking).  */
  1762.       set_sect_size
  1763.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  1764.       set_sect_start = data_start + data_size;
  1765.       data_size += set_sect_size;
  1766.       setv_fill_count = 0;
  1767.     }
  1768.  
  1769.   /* Compute start addresses of each file's sections and symbols.  */
  1770.  
  1771.   relocate_file_addresses ( file_table);
  1772.  
  1773.   /* Now, for each ld_symbol, verify that it is defined globally at most once.
  1774.      Put the global value into the ld_symbol entry.
  1775.      Common symbols are allocated here, in the BSS section.
  1776.      Each defined ld_symbol is given a '->defined' field
  1777.       which is the correct N_ code for its definition,
  1778.       except in the case of common symbols with -r.
  1779.      Then make all the references point at the ld_symbol entry
  1780.      instead of being chained together. */
  1781.  
  1782.   defined_global_sym_count = 0;
  1783.  
  1784.   for (i = 0; i < TABSIZE; i++)
  1785.     {
  1786.       register ld_symbol *sp;
  1787.       for (sp = symtab[i]; sp; sp = sp->link)
  1788.     {
  1789.       /* For each ld_symbol */
  1790.       register struct nlist *p, *next;
  1791.       int defs = 0, com = sp->max_common_size;
  1792.       struct nlist *first_definition;
  1793.       for (p = sp->refs; p; p = next)
  1794.         {
  1795.           register int type = p->n_type;
  1796.  
  1797.           if (SET_ELEMENT_P (type))
  1798.         {
  1799.           if (!defs++)
  1800.             {
  1801.               sp->value = set_sect_start
  1802.             + setv_fill_count++ * sizeof (unsigned long);
  1803.               sp->defined = N_SETV | N_EXT;
  1804.               first_definition = p;
  1805.             }
  1806.           else if ((sp->defined & ~N_EXT) != N_SETV)
  1807.             {
  1808.               sp->multiply_defined = 1;
  1809.               multiple_def_count++;
  1810.             }
  1811.           setv_fill_count++;
  1812.         }
  1813.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  1814.         {
  1815.           /* non-common definition */
  1816.           if (defs++ && sp->value != p->n_value)
  1817.             {
  1818.               sp->multiply_defined = 1;
  1819.               multiple_def_count++;
  1820.             }
  1821.           sp->value = p->n_value;
  1822.           sp->defined = type;
  1823.           first_definition = p;
  1824.         }
  1825.           next = (struct nlist *) p->n_un.n_name;
  1826.           p->n_un.n_name = (char *) sp;
  1827.         }
  1828.       /* Allocate as common if defined as common and not defined for real */
  1829.       if (com && !defs)
  1830.         {
  1831.         return -1;    /* this is not allowed when loading for execution ! */
  1832.         {
  1833.           int align = sizeof (int);
  1834.  
  1835.           /* Round up to nearest sizeof (int).  I don't know
  1836.              whether this is necessary or not (given that
  1837.              alignment is taken care of later), but it's
  1838.              traditional, so I'll leave it in.  Note that if
  1839.              this size alignment is ever removed, ALIGN above
  1840.              will have to be initialized to 1 instead of
  1841.              sizeof (int).  */
  1842.  
  1843.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  1844.  
  1845.           while (!(com & align))
  1846.             align <<= 1;
  1847.  
  1848.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  1849.  
  1850.           bss_size = ((((bss_size + data_size + data_start)
  1851.                   + (align - 1)) & (- align))
  1852.                   - data_size - data_start);
  1853.  
  1854.           sp->value = data_start + data_size + bss_size;
  1855.           sp->defined = N_BSS | N_EXT;
  1856.           bss_size += com;
  1857.         }
  1858.         }
  1859.       /* Set length word at front of vector and zero byte at end.
  1860.          Reverse the vector itself to put it in file order.  */
  1861.       if ((sp->defined & ~N_EXT) == N_SETV)
  1862.         {
  1863.           unsigned long length_word_index
  1864.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  1865.           unsigned long i, tmp;
  1866.  
  1867.           setv_fill_count++;
  1868.         }
  1869.       if (sp->defined)
  1870.         defined_global_sym_count++;
  1871.     }
  1872.     }
  1873.  
  1874.   if (end_symbol)        /* These are null if -r.  */
  1875.     {
  1876.       etext_symbol->value = text_size + text_start;
  1877.       edata_symbol->value = data_start + data_size;
  1878.       end_symbol->value = data_start + data_size + bss_size;
  1879.     }
  1880. return 0;
  1881. }
  1882.  
  1883. /* Determine where the sections of ENTRY go into the output file,
  1884.    whose total section sizes are already known.
  1885.    Also relocate the addresses of the file's local and debugger symbols.  */
  1886.  
  1887. void
  1888. relocate_file_addresses (entry)
  1889.      register struct file_entry *entry;
  1890. {
  1891.   entry->text_start_address += text_start;
  1892.   /* Note that `data_start' and `data_size' have not yet been
  1893.      adjusted for `data_pad'.  If they had been, we would get the wrong
  1894.      results here.  */
  1895.   entry->data_start_address += data_start;
  1896.   entry->bss_start_address += data_start + data_size;
  1897.  
  1898.   {
  1899.     register struct nlist *p;
  1900.     register struct nlist *end
  1901.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1902.  
  1903.     for (p = entry->symbols; p < end; p++)
  1904.       {
  1905.     /* If this belongs to a section, update it by the section's start address */
  1906.     register int type = p->n_type & N_TYPE;
  1907.  
  1908.     switch (type)
  1909.       {
  1910.       case N_TEXT:
  1911.       case N_SETT:
  1912.         p->n_value += entry->text_start_address;
  1913.         break;
  1914.       case N_DATA:
  1915.       case N_SETV:
  1916.       case N_SETD:
  1917.         /* A ld_symbol whose value is in the data section
  1918.            is present in the input file as if the data section
  1919.            started at an address equal to the length of the file's text.  */
  1920.         p->n_value += entry->data_start_address - entry->header.a_text;
  1921.         break;
  1922.       case N_BSS:
  1923.       case N_SETB:
  1924.         /* likewise for symbols with value in BSS.  */
  1925.         p->n_value += entry->bss_start_address
  1926.           - entry->header.a_text - entry->header.a_data;
  1927.         break;
  1928.       }
  1929.       }
  1930.   }
  1931. }
  1932.  
  1933.  
  1934.  
  1935.  
  1936. int
  1937. text_offset (entry)
  1938.      struct file_entry *entry;
  1939. {
  1940.   return entry->starting_offset + N_TXTOFF (entry->header);
  1941. }
  1942.  
  1943. /* Relocate ENTRY's text or data section contents.
  1944.    DATA is the address of the contents, in core.
  1945.    DATA_SIZE is the length of the contents.
  1946.    PC_RELOCATION is the difference between the address of the contents
  1947.      in the output file and its address in the input file.
  1948.    RELOC_INFO is the address of the relocation info, in core.
  1949.    RELOC_SIZE is its length in bytes.  */
  1950. /* This version is about to be severly hacked by Randy.  Hope it
  1951.    works afterwards. */
  1952. int
  1953. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  1954.      char *data;
  1955.      struct relocation_info *reloc_info;
  1956.      struct file_entry *entry;
  1957.      int pc_relocation;
  1958.      int data_size;
  1959.      int reloc_size;
  1960. {
  1961.   register struct relocation_info *p = reloc_info;
  1962.   struct relocation_info *end
  1963.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  1964.   int text_relocation = entry->text_start_address;
  1965.   int data_relocation = entry->data_start_address - entry->header.a_text;
  1966.   int bss_relocation
  1967.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  1968.  
  1969.   for (; p < end; p++)
  1970.     {
  1971.       register int relocation = 0;
  1972.       register int addr = RELOC_ADDRESS(p);
  1973.       register unsigned int mask = 0;
  1974.  
  1975.       if (addr >= data_size)
  1976.     return fatal ("relocation address out of range");
  1977.  
  1978.       if (RELOC_EXTERN_P(p))
  1979.     {
  1980.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  1981.       ld_symbol *sp = ((ld_symbol *)
  1982.             (((struct nlist *)
  1983.               (((char *)entry->symbols) + symindex))
  1984.              ->n_un.n_name));
  1985.  
  1986. #ifdef N_INDR
  1987.       /* Resolve indirection */
  1988.       if ((sp->defined & ~N_EXT) == N_INDR)
  1989.         sp = (ld_symbol *) sp->value;
  1990. #endif
  1991.  
  1992.       if (symindex >= entry->header.a_syms)
  1993.         return fatal ("relocation symbolnum out of range");
  1994.  
  1995.       /* If the ld_symbol is undefined, leave it at zero.  */
  1996.       if (! sp->defined)
  1997.         relocation = 0;
  1998.       else
  1999.         relocation = sp->value;
  2000.     }
  2001.       else switch (RELOC_TYPE(p))
  2002.     {
  2003.     case N_TEXT:
  2004.     case N_TEXT | N_EXT:
  2005.       relocation = text_relocation;
  2006.       break;
  2007.  
  2008.     case N_DATA:
  2009.     case N_DATA | N_EXT:
  2010.       /* A word that points to beginning of the the data section
  2011.          initially contains not 0 but rather the "address" of that section
  2012.          in the input file, which is the length of the file's text.  */
  2013.       relocation = data_relocation;
  2014.       break;
  2015.  
  2016.     case N_BSS:
  2017.     case N_BSS | N_EXT:
  2018.       /* Similarly, an input word pointing to the beginning of the bss
  2019.          initially contains the length of text plus data of the file.  */
  2020.       relocation = bss_relocation;
  2021.       break;
  2022.  
  2023.     case N_ABS:
  2024.     case N_ABS | N_EXT:
  2025.       /* Don't know why this code would occur, but apparently it does.  */
  2026.       break;
  2027.  
  2028.     case 0:
  2029.     /* bodge */
  2030.     break;
  2031.  
  2032.     default:
  2033.       return fatal ("nonexternal relocation code invalid");
  2034.     }
  2035.  
  2036. #ifdef RELOC_ADD_EXTRA
  2037.       relocation += RELOC_ADD_EXTRA(p);
  2038.       if (relocatable_output)
  2039.     {
  2040.       /* Non-PC relative relocations which are absolute
  2041.          or which have become non-external now have fixed
  2042.          relocations.  Set the ADD_EXTRA of this relocation
  2043.          to be the relocation we have now determined.  */
  2044.       if (! RELOC_PCREL_P (p))
  2045.         {
  2046.           if ((int)p->r_type <= RELOC_32
  2047.           || RELOC_EXTERN_P (p) == 0)
  2048.         RELOC_ADD_EXTRA (p) = relocation;
  2049.         }
  2050.       /* External PC-relative relocations continue to move around;
  2051.          update their relocations by the amount they have moved
  2052.          so far.  */
  2053.       else if (RELOC_EXTERN_P (p))
  2054.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  2055.       continue;
  2056.     }
  2057. #endif
  2058.  
  2059.       if (RELOC_PCREL_P(p))
  2060.     relocation -= pc_relocation;
  2061.  
  2062.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  2063.  
  2064.       /* Unshifted mask for relocation */
  2065.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  2066.       mask |= mask - 1;
  2067.       relocation &= mask;
  2068.  
  2069.       /* Shift everything up to where it's going to be used */
  2070.       relocation <<= RELOC_TARGET_BITPOS(p);
  2071.       mask <<= RELOC_TARGET_BITPOS(p);
  2072.  
  2073.       switch (RELOC_TARGET_SIZE(p))
  2074.     {
  2075.     case 0:
  2076.       if (RELOC_MEMORY_SUB_P(p))
  2077.         relocation -= mask & *(char *) (data + addr);
  2078.       else if (RELOC_MEMORY_ADD_P(p))
  2079.         relocation += mask & *(char *) (data + addr);
  2080.       *(char *) (data + addr) &= ~mask;
  2081.       *(char *) (data + addr) |= relocation;
  2082.       break;
  2083.  
  2084.     case 1:
  2085.       if (RELOC_MEMORY_SUB_P(p))
  2086.         relocation -= mask & *(short *) (data + addr);
  2087.       else if (RELOC_MEMORY_ADD_P(p))
  2088.         relocation += mask & *(short *) (data + addr);
  2089.       *(short *) (data + addr) &= ~mask;
  2090.       *(short *) (data + addr) |= relocation;
  2091.       break;
  2092.  
  2093.     case 2:
  2094. #ifndef _CROSS_TARGET_ARCH
  2095.       if (RELOC_MEMORY_SUB_P(p))
  2096.         relocation -= mask & *(long *) (data + addr);
  2097.       else if (RELOC_MEMORY_ADD_P(p))
  2098.         relocation += mask & *(long *) (data + addr);
  2099.       *(long *) (data + addr) &= ~mask;
  2100.       *(long *) (data + addr) |= relocation;
  2101. #else
  2102.     /* Handle long word alignment requirements of SPARC architecture */
  2103.     /* WARNING:  This fix makes an assumption on byte ordering */
  2104.     /* Marc Ullman, Stanford University    Nov. 1 1989  */
  2105.       if (RELOC_MEMORY_SUB_P(p)) {
  2106.         relocation -= mask & 
  2107.           ((*(unsigned short *) (data + addr) << 16) |
  2108.         *(unsigned short *) (data + addr + 2));
  2109.       } else if (RELOC_MEMORY_ADD_P(p)) {
  2110.         relocation += mask &
  2111.           ((*(unsigned short *) (data + addr) << 16) |
  2112.         *(unsigned short *) (data + addr + 2));
  2113.       }
  2114.       *(unsigned short *) (data + addr)     &= (~mask >> 16);
  2115.       *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff);
  2116.       *(unsigned short *) (data + addr)     |= (relocation >> 16);
  2117.       *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff);
  2118. #endif
  2119.       break;
  2120.  
  2121.     default:
  2122.       return fatal ("Unimplemented relocation field length");
  2123.     }
  2124.     }
  2125. return 0;
  2126. }
  2127.  
  2128.  
  2129. void write_file_syms ();
  2130. void write_string_table ();
  2131.  
  2132. /* Add the string NAME to the output file string table.
  2133.    Record it in `strtab_vector' to be output later.
  2134.    Return the index within the string table that this string will have.  */
  2135.  
  2136. int
  2137. assign_string_table_index (name)
  2138.      char *name;
  2139. {
  2140.   register int index = strtab_size;
  2141.   register int len = strlen (name) + 1;
  2142.  
  2143.   strtab_size += len;
  2144.   strtab_vector[strtab_index] = name;
  2145.   strtab_lens[strtab_index++] = len;
  2146.  
  2147.   return index;
  2148. }
  2149.  
  2150. /* Create the ld_symbol table entries for `etext', `edata' and `end'.  */
  2151.  
  2152. void
  2153. symtab_init ()
  2154.     {
  2155. #ifndef nounderscore
  2156.   edata_symbol = getsym ("_edata");
  2157.   etext_symbol = getsym ("_etext");
  2158.   end_symbol = getsym ("_end");
  2159. #else
  2160.   edata_symbol = getsym ("edata");
  2161.   etext_symbol = getsym ("etext");
  2162.   end_symbol = getsym ("end");
  2163. #endif
  2164.  
  2165.   edata_symbol->defined = N_DATA | N_EXT;
  2166.   etext_symbol->defined = N_TEXT | N_EXT;
  2167.   end_symbol->defined = N_BSS | N_EXT;
  2168.  
  2169.   edata_symbol->referenced = 1;
  2170.   etext_symbol->referenced = 1;
  2171.   end_symbol->referenced = 1;
  2172. }
  2173.  
  2174. /* Compute the hash code for ld_symbol name KEY.  */
  2175.  
  2176. int
  2177. hash_string (key)
  2178.      char *key;
  2179. {
  2180.   register char *cp;
  2181.   register int k;
  2182.  
  2183.   cp = key;
  2184.   k = 0;
  2185.   while (*cp)
  2186.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  2187.  
  2188.   return k;
  2189. }
  2190.  
  2191. /* Get the ld_symbol table entry for the global ld_symbol named KEY.
  2192.    Create one if there is none.  */
  2193.  
  2194. ld_symbol *
  2195. getsym (key)
  2196.      char *key;
  2197. {
  2198.   register int hashval;
  2199.   register ld_symbol *bp;
  2200.  
  2201.   /* Determine the proper bucket.  */
  2202.  
  2203.   hashval = hash_string (key) % TABSIZE;
  2204.  
  2205.   /* Search the bucket.  */
  2206.  
  2207.   for (bp = symtab[hashval]; bp; bp = bp->link)
  2208.     if (! strcmp (key, bp->name))
  2209.       return bp;
  2210.  
  2211.   /* Nothing was found; create a new ld_symbol table entry.  */
  2212.  
  2213.   bp = (ld_symbol *) xmalloc (sizeof (ld_symbol));
  2214.   bp->refs = 0;
  2215.   bp->name = (char *) xmalloc (strlen (key) + 1);
  2216.   strcpy (bp->name, key);
  2217.   bp->defined = 0;
  2218.   bp->referenced = 0;
  2219.   bp->trace = 0;
  2220.   bp->value = 0;
  2221.   bp->max_common_size = 0;
  2222.   bp->warning = 0;
  2223.   bp->undef_refs = 0;
  2224.   bp->multiply_defined = 0;
  2225.  
  2226.   /* Add the entry to the bucket.  */
  2227.  
  2228.   bp->link = symtab[hashval];
  2229.   symtab[hashval] = bp;
  2230.  
  2231.   ++num_hash_tab_syms;
  2232.  
  2233.   return bp;
  2234. }
  2235.  
  2236. /* Like `getsym' but return 0 if the ld_symbol is not already known.  */
  2237.  
  2238. ld_symbol *
  2239. getsym_soft (key)
  2240.      char *key;
  2241. {
  2242.   register int hashval;
  2243.   register ld_symbol *bp;
  2244.  
  2245.   /* Determine which bucket.  */
  2246.  
  2247.   hashval = hash_string (key) % TABSIZE;
  2248.  
  2249.   /* Search the bucket.  */
  2250.  
  2251.   for (bp = symtab[hashval]; bp; bp = bp->link)
  2252.     if (! strcmp (key, bp->name))
  2253.       return bp;
  2254.  
  2255.   return 0;
  2256. }
  2257.  
  2258.